package com.barray.log_monitor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

public class LogButtonManager {
    public static final int SETTING_REQUEST_CODE = 100;
    private static class LogButton extends AppCompatImageView implements View.OnTouchListener {
        private WindowManager.LayoutParams mParams;
        public  WindowManager.LayoutParams getWindowManagerParams(){
            return mParams;
        }
        private void initView() {
            mParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSPARENT);
            //设置悬浮窗坐标
            mParams.x = 100;
            mParams.y = 100;
            //表示该Window无需获取焦点，也不需要接收输入事件
            mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            mParams.gravity = Gravity.START | Gravity.TOP;
            mParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            setOnTouchListener(this);
        }


        public LogButton(Context context) {
            this(context, null);
        }

        public LogButton(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public LogButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initView();
        }


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int rawX=(int)event.getRawX();
            int rawY=(int)event.getRawY();
            if(event.getAction() == MotionEvent.ACTION_MOVE
                    || event.getAction() == MotionEvent.ACTION_UP){
                mParams.x=rawX - getWidth()/2;
                mParams.y=rawY - getHeight()/2;
                logButtonManager.updateViewLayout(this,mParams);
            }
            return false;
        }
    }

    private LogButton mFloatingImageView;
    private WindowManager mWindowManager;
    private static LogButtonManager logButtonManager;
    private LogButtonManager(){

    }

    public void updateViewLayout(View view, ViewGroup.LayoutParams params){
        if(mWindowManager == null || view == null || params == null) return;
        mWindowManager.updateViewLayout(view,params);
    }




    public static LogButtonManager getInstance(){
        if(logButtonManager == null){
            logButtonManager = new LogButtonManager();
        }
        return logButtonManager;
    }

    public boolean addButtonView(Activity activity) {
        if (activity == null) return false;
        if (!Settings.canDrawOverlays(activity)) {
            AlertDialog alertDialog =
                    new AlertDialog.Builder(activity)
                            .setMessage("亲爱的开发者，你要查看日志吗？\n那就请打开浮框权限吧！")
                            .setPositiveButton("确定", (dialog, which) -> {
                                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                        Uri.parse("package:" + activity.getPackageName()));
                                activity.startActivityForResult(intent,SETTING_REQUEST_CODE);
                            })
                            .setCancelable(false)
                            .create();
            alertDialog.show();
            return false;
        }
        if (mFloatingImageView == null) {
            mFloatingImageView = new LogButton(activity.getApplicationContext());
            mFloatingImageView.setImageResource(R.drawable.shape_icon_logcat);
            mWindowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
            mWindowManager.addView(mFloatingImageView,mFloatingImageView.getWindowManagerParams());
            mFloatingImageView.setOnClickListener(v -> {
                Intent intent = new Intent(activity,LogPageActivity.class);
                activity.startActivity(intent);
            });
        }
        return true;
    }

    public void removeButtonView(){
        if(mFloatingImageView == null || mWindowManager == null) return;
        mWindowManager.removeView(mFloatingImageView);
        mFloatingImageView = null;
    }
}
